package com.alkin.thing.project.iot.service.impl;

import com.alkin.thing.common.utils.StringUtils;
import com.alkin.thing.project.iot.domain.View;
import com.alkin.thing.project.iot.domain.ViewLink;
import com.alkin.thing.project.iot.mapper.ViewLinkMapper;
import com.alkin.thing.project.iot.mapper.ViewMapper;
import com.alkin.thing.project.iot.service.IViewService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * web可视化页面Service业务层处理
 *
 * @author alkin
 * @date 2020-02-25
 */
@Service
public class ViewServiceImpl extends ServiceImpl<ViewMapper, View> implements IViewService {
    @Autowired
    private ViewMapper viewMapper;

    @Autowired
    private ViewLinkMapper viewLinkMapper;

    @Override
    @Transactional
    public void saveViewLink(ViewLink viewLink) {
        if (StringUtils.isEmpty(viewLink.getBizType()) || viewLink.getBizId() == null) {
            return;
        }
        // 先删除后插入
        viewLinkMapper.delete(new QueryWrapper<ViewLink>()
                .eq("biz_type", viewLink.getBizType())
                .eq("biz_id", viewLink.getBizId())
        );
        if (viewLink.getViewId() != null) {
            viewLinkMapper.insert(viewLink);
        }

    }

    @Override
    public View getByBizTypeAndId(String bizType, Long bizId) {
        return viewMapper.getByBizTypeAndId(bizType, bizId);
    }
}
